//
//  vdTMXLayer.m
//  Void Dead
//
//  Created by Sidney Just on 02.02.10.
//
//  Copyright © 2009 by Sidney Just
//  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
//  documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
//  the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 
//  and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
//  INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
//  PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
//  FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
//  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#import "vdTMXLayer.h"

@implementation vdTMXLayer
@synthesize tiles, tileHeight, tileWidth, layerWidth, layerHeight, firstGid, layerName;

- (void)createMap {
	int tileCount = 0;
	CFByteOrder byteOrder = CFByteOrderGetCurrent();
	
	for(int i=0; i<layerHeight*layerWidth; i++)
	{
		unsigned int tid = tiles[i];
		if(byteOrder == CFByteOrderBigEndian)
			tid = CFSwapInt32(tid);
		
		if(tid > 0)
			tileCount++;
	}
	
	[self setAppendSize:tileCount+1];
	unsigned int i = 0;
	
	for(int y=0; y<layerHeight; y++)
	{
		for(int x=0; x<layerWidth; x++)
		{
			unsigned int tid = tiles[i];
			if(byteOrder == CFByteOrderBigEndian)
				tid = CFSwapInt32(tid);
			
			if(tid > 0) {
				tid -= firstGid;
				
				int altasPosX = tid%(int)(texture.dimension_x/tileWidth);
				int altasPosY = tid/(int)(texture.dimension_x/tileWidth);
				
				[self addShape:CGPointMake(x*tileWidth, y*tileHeight) 
					withAtlas:CGRectMake(altasPosX*tileWidth, altasPosY*tileHeight, tileWidth, tileHeight)
					forKey:i];
			}
			
			i++;
		}
	}
}

- (unsigned int)getTileID:(CGPoint)_point {
	unsigned int i = _point.x + (_point.y *layerWidth);
	
	return tiles[i];
}

- (void)setTileID:(CGPoint)_point :(unsigned int)gid {
	unsigned int i = _point.x + (_point.y *layerWidth);
	tiles[i] = gid;
	
	CFByteOrder byteOrder = CFByteOrderGetCurrent();
	
	unsigned int tid = tiles[i];
	if(byteOrder == CFByteOrderBigEndian)
		tid = CFSwapInt32(tid);
	
	if(tid > 0) {
		tid -= firstGid;
		
		int altasPosX = tid%(int)(texture.dimension_x/tileWidth);
		int altasPosY = tid/(int)(texture.dimension_x/tileWidth);

		if([self getShapeByKey:i])
		{
			[self updateAtlas:CGRectMake(altasPosX*tileWidth, altasPosY*tileHeight, tileWidth, tileHeight) forShape:[self getShapeByKey:i]];
		} else {
			[self addShape:CGPointMake(_point.x*tileWidth, _point.y*tileHeight) 
				 withAtlas:CGRectMake(altasPosX*tileWidth, altasPosY*tileHeight, tileWidth, tileHeight)
					forKey:i];
		}

	} else {
		[self removeShape:[self getShapeByKey:i]];
	}

}

- (void)changeAllTilesWithGID:(unsigned int)oGid to:(unsigned int)nGid {
	unsigned int replaced = 0;
	
	CFByteOrder byteOrder = CFByteOrderGetCurrent();
	unsigned int i = 0;
	
	for(int y=0; y<layerHeight; y++)
	{
		for(int x=0; x<layerWidth; x++)
		{
			unsigned int tid = tiles[i];
			if(byteOrder == CFByteOrderBigEndian)
				tid = CFSwapInt32(tid);
			
			if(tid == oGid)
			{
				[self setTileID:CGPointMake(x, y) :nGid];
				replaced++;
			}
			
			i++;
		}
	}
	
	//vdLog(@"Changed %i tiles with GID %i to GID %i on %@", replaced, oGid, nGid, self);
}

- (void)setTexture:(vdTexNode *)_tex {
	texture = _tex;
}

- (id)initWithName:(NSString *)_name {
	if(self = [super init])
	{
		firstGid = 1;
		layerName = [[NSString alloc] initWithString:_name];
	}
	return self;
}

- (void)dealloc {
	free(tiles);
	
	[layerName release];
	[super dealloc];
}

@end
